package com.braveman.im.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.braveman.im.R;
import com.braveman.im.ui.ChatActivity;
import com.braveman.im.ui.adapter.GroupAdapter;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;


public class GroupImActivity extends BaseActivity {

    private SwipeRefreshLayout mSwipeLayout;
    private ListView mList;
    protected List<EMGroup> grouplist;
    private GroupAdapter groupAdapter;
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mSwipeLayout.setRefreshing(false);
            switch (msg.what) {
                case 0:
                    refresh();
                    break;
                case 1:
                    Toast.makeText(GroupImActivity.this, R.string.Failed_to_get_group_chat_information, Toast.LENGTH_LONG).show();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("群组列表").addActions(new TitleBarView.Action("新建群组") {
            @Override
            public void performAction(View view) {
                ActivityUtils.startActivity(GroupImActivity.this, NewGroupActivity.class);
            }
        });
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_group_im;
    }

    @Override
    protected void initChildView() {
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
        mList = (ListView) findViewById(R.id.list);

    }

    @Override
    protected void initChildData() {
        refresh();
    }

    @Override
    protected void setChildViewListener() {
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            EMClient.getInstance().groupManager().getJoinedGroupsFromServer();
                            handler.sendEmptyMessage(0);
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                            handler.sendEmptyMessage(1);
                        }
                    }
                }.start();
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 跳转到聊天界面，开始聊天
                String chatId = grouplist.get(i).getGroupId();
                Intent intent = new Intent(GroupImActivity.this, ChatActivity.class);
                intent.putExtra("ec_chat_id", chatId);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);


        switch (v.getId()) {


        }


    }

    private void refresh() {
//        grouplist = EMClient.getInstance().groupManager().getAllGroups();
//        groupAdapter = new GroupAdapter(this, 1, grouplist);
//        mList.setAdapter(groupAdapter);
//        groupAdapter.notifyDataSetChanged();
    }


}
